package com.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ServletTest", urlPatterns = "/test")
public class ServletTest extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //设置response的编码
        response.setContentType("text/html;charset=UTF-8");
//        request.getRequestDispatcher("/Head").include(request, response);


        //创建Cookie对象，指定名称和值
        Cookie cookie = new Cookie("username", "zhongfucheng");

        //设置Cookie的时间
        cookie.setMaxAge(1000);

        //向浏览器给一个Cookie
        response.addCookie(cookie);

        response.getWriter().write("我已经向浏览器发送了一个Cookie." + "\n");

//        request.getRequestDispatcher("/Foot").include(request, response);
        System.out.println("test servlet is finished");
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }


}
